package com.af.community.smart.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.af.community.smart.base.*
import com.af.community.smart.business.R
import com.af.community.smart.model.PeopleData
import com.af.community.smart.utils.scaleImage
import com.af.community.smart.utils.toFormatString
import com.jeremyliao.liveeventbus.LiveEventBus
import com.sunfusheng.GlideImageView
import kotlinx.android.synthetic.main.fragment_people_detail2.*
import net.idik.lib.slimadapter.SlimAdapter
import net.moyokoo.diooto.Diooto
import net.moyokoo.diooto.config.DiootoConfig
import org.jetbrains.anko.textColor

class PeopleDetail2Fragment : BaseFragment() {

    private lateinit var mData: PeopleData

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_people_detail2, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        LiveEventBus.get("peopie_edit", PeopleData::class.java)
            .observe(this, Observer {
                mData = it
                setDetailData(it)
                (activity as OnFragmentListener).onDataChanged(it)
            })
    }

    override fun initTitle() {
        mData = arguments!!.getSerializable("data") as PeopleData

        img_list.apply {
            layoutManager = LinearLayoutManager(
                activity,
                LinearLayoutManager.HORIZONTAL,
                false
            )
        }

        mAdapter = SlimAdapter.create()
            .register<String>(R.layout.item_people_img1) { inner, injector ->
                injector.with<GlideImageView>(R.id.item_people) { it.loadRoundedImage(inner.scaleImage()) }
                    .clicked(R.id.item_people) {
                        val imgs = (mData.prove.orEmpty())
                            .replace(";", ",")
                            .trimEnd(',')
                            .split(",")

                        Diooto(activity)
                            .type(DiootoConfig.PHOTO)
                            .urls(imgs.toTypedArray())
                            .position(imgs.indexOf(inner))
                            .views(img_list, R.id.item_people)
                            .loadPhotoBeforeShowBigImage { view, index ->
                                view.displayImage(imgs[index].scaleImage())
                            }
                            .start()
                    }
            }
            .attachTo(img_list)

        people_img.oneClick {
            if (!mData.avatar.isNullOrEmpty()) {
                val img = mData.avatar.orEmpty().trimEnd(',')

                Diooto(activity)
                    .type(DiootoConfig.PHOTO)
                    .urls(img)
                    .position(0)
                    .views(it)
                    .loadPhotoBeforeShowBigImage { view, _ ->
                        view.displayImage(img.scaleImage())
                    }
                    .start()
            }
        }

        setDetailData(mData)
    }

    private fun setDetailData(data: PeopleData) {
        val isShow = arguments!!.getBoolean("isShow", false)
        if (isShow) {
            people_status.visible()
            people_status.text = when (data.status) {
                "1" -> {
                    people_status.textColor = activity!!.getColorEx(R.color.colorAccent)
                    "已审核"
                }
                "9" -> {
                    people_status.textColor = activity!!.getColorEx(R.color.red)
                    "待审核"
                }
                else -> ""
            }
        }

        people_grid.text = data.gridName
        people_znum.text = data.housNumber
        people_ztype.text = data.housType
        people_mname.text = data.owner
        people_mid.text = data.ownerID
        people_hnum.text = data.doorNumber
        people_lnum.text = data.zoneNumber

        people_name.text = data.userName
        people_sex.text =
            when (data.sex) {
                "0" -> "女"
                "1" -> "男"
                else -> ""
            }
        people_nation.text = data.national
        people_place.text = data.birthplace
        people_birth.text = data.birthday
        people_id.text = data.account

        people_culture.text = data.education
        people_type.text = data.residenceType
        people_relation.text = data.relation
        people_level.text = data.titleLevel

        people_in.text = data.intotime.toFormatString(eFormat = "yyyy-MM-dd")
        people_reason.text = data.toLiveFor
        people_state.text = data.temporaryResidence
        people_time1.text = data.temporarySignTime.toFormatString(eFormat = "yyyy-MM-dd")
        people_time2.text = data.temporaryEndTime.toFormatString(eFormat = "yyyy-MM-dd")
        people_tel1.text = data.phone
        people_addr1.text = data.address
        people_job.text = data.professional
        people_unit.text = data.workName
        people_addr2.text = data.workAddress
        people_tel2.text = data.workTel
        people_memo.text = data.remark

        val mSpecial = data.specialType.orEmpty()
        people_can.text = if ("201" in mSpecial) "是" else "否"
        people_di.text = if ("202" in mSpecial) "是" else "否"
        people_shi.text = if ("205" in mSpecial) "是" else "否"

        if (data.avatar.isNullOrEmpty()) people_img.gone()
        else {
            people_img.visible()
            people_img.loadRoundedImage((data.avatar.orEmpty().trimEnd(',')).scaleImage())
        }

        val proveUrl = data.prove.orEmpty()
        if (proveUrl.isNotEmpty()) {
            val urls = proveUrl
                .replace(";", ",")
                .trimEnd(',')
            mAdapter.updateData(urls.split(","))
        }
    }

}
